controllers.controller('disburseCtrl', ['$scope', '$http', '$rootScope', '$modal', '$window', 'Disburse', 'AlertService',
    function ($scope, $http, $rootScope, $modal, $window, Disburse, AlertService) {
        $scope.resetCurrentPageName("订单维护");
        $scope.utils = Food.utils;
        $scope.pageSize = 10;
        $scope.currentPage = 1;
        $scope.disburseList = Disburse.disburseList;
        $scope.disburseNo;
        $scope.outOrderNo;
        $scope.status;
        $scope.currentProperty = Disburse.currentProperty;

        $scope.queryList = function () {
            Disburse.queryList($scope.disburseNo, $scope.rechargeOrderNo, $scope.buyerMemberName, $scope.status, $scope.startDate, $scope.endDate, $scope.currentPage, $scope.pageSize, function () {
                $scope.totalItems = Disburse.totalSceneCount;
            });
        };

        $scope.initSendGoods = function (disburse) {

            Disburse.currentDisburse = disburse;
            $rootScope.contextIsModal = true;
            $rootScope.modalIns = $modal.open({
                templateUrl: './partials/order/sendGoods.html',
                animation: true,
                controller: 'sendGoodsCtrl',
                windowClass: 'app-modal-window',
                size: 'lg',
                resolve: {}
            });
            $rootScope.ok = function () {
                //callback();
                var removeIndex;
                $.each(Disburse.disburseList, function (index, result) {
                    if (result['disburseNo'] == disburse['disburseNo']) {
                        removeIndex = index;
                    }
                });
                Disburse.disburseList.splice(removeIndex, 1);

                $rootScope.modalIns.dismiss();
            };
            $rootScope.cancel = function () {
                $rootScope.modalIns.dismiss();
            };
            $rootScope.modalIns.result.then(function () {
                $rootScope.contextIsModal = false;

            }, function () {
                $rootScope.contextIsModal = false;
            });

        };

        $scope.queryList();

        var operateTemplate = "<div ng-if='row.entity.status == 1'><button class='btn btn-primary btn-sm' ng-click='grid.appScope.initSendGoods(row.entity)'>发货</button></div>";

        //var operateTemplate = "<button class='btn btn-primary btn-lg' data-toggle='modal' data-target='#myModal'>发货</button>"

        var snapshot = "<table>" +
            "<tr ng-repeat='x in row.entity.snapshots'>" +
            "<td><div class='ui-grid-cell-contents'>{{ x.skuName }}</div></td><td>{{ x.quantity }}</td><td>{{ x.snapshotStatusName }}</td>" +
            "<td>{{ x.price }}</td><td><div class='ui-grid-cell-contents'><img src='{{x.pic}}' style='width: 50px'/></div></td>" +
            "</tr>" +
            "</table>";

        $scope.gridOptions = {
            data: 'disburseList',
            enableColumnMenus: false,
            enableColumnResizing: true,
            rowHeight: 50,
            columnDefs: [
                {field: 'disburseNo', displayName: '消费订单号', width: 210},
                {field: 'pingxxId', displayName: 'ping++订单号', width: 240},
                {field: 'buyerMemberName', displayName: '会员账号', width: 120},
                {field: 'totalPriceStr', displayName: '总金额', width: 80},
                {field: 'modeName', displayName: '付款方式', width: 80},
                {field: 'statusName', displayName: '状态', width: 100},
                {field: 'logisticsCompany', displayName: '快递公司', width: 80},
                {field: 'logisticsNo', displayName: '发货订单号', width: 120},
                {
                    field: 'snapshots',
                    displayName: '商品信息',
                    width: "20%",
                    cellTemplate: snapshot
                },
                {
                    field: 'id',
                    displayName: '操作',
                    width: "200",
                    cellTemplate: operateTemplate
                }
            ]
        };
    }]);


//更新订单
controllers.controller('sendGoodsCtrl', ['$scope', '$rootScope', '$window', '$modal', 'Disburse', 'AlertService',
    function ($scope, $rootScope, $window, $modal, Disburse, AlertService) {
        $scope.resetCurrentPageName("发货管理");
        $scope.currentDisburse = Disburse.currentDisburse;


        $scope.sendGoods = function (disburse) {

            Disburse.sendGoods($scope.currentDisburse, $scope.logisticsCompany, $scope.logisticsNo, function () {
                AlertService.addAlert({type: 'success', msg: '发货成功! '}, 1000);
                //$window.location.href = "#tran/disburseList";
                //上下文放到这里
                //$rootScope.selectedItemList = [];
                $rootScope.ok();
            });
            //回调函数

        };


    }]);


